import sympy
from sympy import *

'''
# sympy库的导入
from sympy import symbols
import sympy as sp
# 定义符号
x=sp.symbols('x')
x,y,z=symbols('x,y,z')
print("x的变量类型为",type(x))

from sympy import symbols
s=symbols('s0:10')
print('s的数据类型是:',type(s))
print('s的长度是:',len(s))
print('s=',s)
print('s[5]=',s[5])

# 限定符号对象的取值范围
x = sympy.symbols('x',integer=True)
y = sympy.symbols('y',real=True)
z = sympy.symbols('z',complex=True)
w = sympy.symbols('w',positive=True)
u =sympy.symbols('u',negtive=True)
# y = sympy.symbols('y')
# assumptions0 是 SymPy 库中的一个属性，返回一个字典，其中包含对符号变量所做的所有假设
print(y.assumptions0)
print(type(y.assumptions0))

# sympy符号计算
x = sympy.exp(sympy.I*sympy.pi)+1
print(x)

# evalf返回默认15位小数的浮点型数值
a=[sympy.E,sympy.I,sympy.pi,sympy.oo]
for i in a:
    print(i.evalf(12))

b=sympy.E
c=b**5
print(c.evalf())

# 使用sympy.var()函数定义符号变量
a,b,c=sympy.var('a b c')
s=a*sympy.sin(b*sympy.sqrt(c))
print('s=',s)

# subs()函数进行符号替换
x,y= symbols('x y')
exp=x**2+1
print("Before Substitution : {}".format(exp))
res_exp=exp.subs(x,y**2)
print("After Substitution : {}".format(res_exp))

x1,x2=symbols('x1,x2')
def func1():
    return pow(x1,2)+2*pow(x2,2)-2*x1*x2-2 *x2
x=symbols('x')
def func2():
    return pow(x,2)+ 2*x

f1 = func1()
print(f1.subs(x1,2).subs(x2,2))
# f1.subs([(x1,2),(x2,2)])
# f1.subs({x1:2,x2:2})
f2 = func2()
print(f2.subs(x,2))

#符号表达式的化简
x=symbols('x')
print(sympy.simplify('x**2+x*2+x'))

s=(x**3+x**2-x-1)/(x**2+2*x+1)
print('s被化简前:',s)
print('s被化简后:',sympy.simplify(s))
t=sympy.sin(2*x)/sympy.cos(x)
print('t被化简前:',t)
print('t被化简后:',sympy.trigsimp(t))
ex=sympy.exp(2*x-2*y)/sympy.exp(x-y)
print('ex被化简前:',ex)
print('ex被化简后:',sympy.powsimp(ex))

# sympy.sympify()函数将字符串转化为符号表达式
str_ex ='x**2+3*x-g'
ex =sympy.sympify(str_ex)
print(ex)

#多项式展开
x,y=symbols('x y')
ex=(x+3)*(x+2)
print('ex =',ex)
print('ex展开后=',sympy.expand(ex))
ex_2=((x+1)*(x-2)-(x-1)*x)
print('ex_2 =',ex_2)
print('ex_2展开后=',sympy.expand(ex_2))

# 因式分解
ex_3=(x**2+2*x+1)
print('ex_3 =',ex_3)
print('ex_3因式分解后=',sympy.factor(ex_3))

#合并同类项
x=sympy.symbols('x')
expr=x*y+x-3+2*x**2-5*x**2 + x**3+(x+y)*(x-y)
sympy.collect(expr,x)
print(sympy.collect(expr,x))

# apart()函数进行多项式的部分分式分解
s=1/((x+2)*(x+1)*(2*x+3))
print('s=',s)
print('化简后的s=',sympy.apart(s,x))

# together()函数进行多项式的合并
print('重新合并后的s=',sympy.together(sympy.apart(s,x),x))
# cancel()函数进行多项式的通分分解
print('重新合并后再展开的结果rs=',sympy.cancel(sympy.together(sympy.apart(s,x),x)),x)

# 函数极限计算
from sympy import limit,var,sin
var('x')
s=sin(x)/abs(x)
t1=limit(s,x,0,'+')
t2=limit(s,x,0,'-')
print(t1,t2)

from sympy import limit,symbols,oo
x=symbols('x')
S=(1+1/x)**x
t1=limit(s,x,-00)
#t1=limit(s,x,-oo)
print(t1)

# 数列极限计算
from sympy import symbols,limit
n = symbols('n', integer=True)
s=(1+1/n)**n
t1=limit(s,n,oo)
print(t1)

from sympy import symbols,limit,sin,pi,oo
n=symbols('n',integer=True)
s=sin(pi*n)
t1=limit(s,x,oo)
print(t1)

from sympy import limit,symbols,sin,pi,oo
n=symbols('n',integer=True)
s=limit(n**2*(1-n*sin(1/n)),n,oo)
s.doit()
print(s)

#导数及偏导数计算
from sympy import var,cos,diff,exp
x,y=var('x,y')
f1=diff(cos(x**2))
#计算关于x的二阶偏导数
f2=diff(exp(x**2+y**2),x,2)
print(f1)
print(f2)

# 方程组求解
from sympy import *
x,y = var('x y')
s=x*y-exp(x)+exp(y)
d=-diff(s,x)/diff(s,y)
di=d.subs({x:0,y:0})
print(d,di)

# 不定积分和定积分计算
from sympy import *
t,x=var('t,x')
s=t*sympy.exp(t**2)
l1=sympy.Integral(s,t)
l1.doit()
print(l1.doit())
l2=sympy.integrate(s,(t,0,1))
#l2.doit()
print(l2.doit())
# 变限积分的计算
s=t*exp(t**2)
l3=integrate(s,(t,0,x**2))
print(l3)

# 级数展开与移除高阶无穷小
from sympy import *
var('x')
f=exp(x)
s1=f.series(x)
print(s1)
s2=f.series(x,0,7)
print(s2)
print(s2.removeO())
s3=f.series(x,1,4)
print(s3)

# solve方程组求解
from sympy import *
x=symbols('x')
y=symbols('y')
print(solve([3*y+5*y-19,4*x-3*y-6],[x,y]))
# 求解方程
x=symbols('x')
print(solve(x**3+x**2+2*x-4,x))

# linsolve求解线性方程组
from sympy import *
x, y, z = symbols("x y z")
print("====默认等式为0的形式 ====")
eq =[x+y+z-2,2*x-y+z+1,x+2*y+2*z-3]
result = linsolve(eq, [x, y, z])
print(result)

# 矩阵形式
print("======矩阵形式 ======")
eq = Matrix(([1,1,1,2],[2,-1,1,-1],[1,2,2,3]))
result = linsolve(eq, [x, y, z])
print(result)
# 增广矩阵形式
print("====增广矩阵形式 =====")
A = Matrix([[1,1,1],[2,-1,1],[1,2,2]])
b = Matrix([[2],[-1],[3]])
system =A, b
result = linsolve(system, x, y, z)
print(result)
# nonlinsolve非线性方程组求解
x, y, z = symbols("x y z")
result=nonlinsolve([x**2+y**2+z**2-1,x-z],[x,y])
print(result)
# dsolve常微分方程求解
from sympy import symbols, Eq, dsolve, sin, Function
x = symbols('x')
y = Function('y')(x)
eq = Eq(y.diff(x, 2) - 2 * y.diff(x) + y, sin(x))  # y" - 2y' + y = sin(x)
solution = dsolve(eq, y)  # 求通解
print(solution)
# sympy符号绘图
from sympy.plotting import plot
from sympy import *
x=symbols('x')
plot(x**2,(x,-12,25),line_color='red')

from sympy.plotting import plot
from sympy import *
x,y,t=var('x,y,t')
plot(sin(t),cos(t),(t,0,2*pi),line_color='blue',title ='sin,cos')
# 绘制二维曲线
from sympy.plotting import plot
from sympy import *
x,y,t = var('x,y,t')
plot((sin(t),(t,0,2*pi)),(cos(t),(t,-2*pi,2*pi)))

#绘制隐函数曲线
# sympy.plot_implicit(expr,x_var=None, y_var=None, adaptive=True,
                    # depth=0,points=300, line_color='blue', show=True,**kwargs)

from sympy import plot_implicit, symbols, Eq
x,y= symbols('x y')
p1=plot_implicit(Eq(x**2 + y**2,16),line_color='red')

#绘制填充图形
from sympy import plot_implicit, symbols
x,y= symbols('x y')
p1=plot_implicit(y>=x**2,line_color='red')

from sympy import plot_implicit, symbols
x, y= symbols('x y')
p1=plot_implicit(abs(x)+abs(y)<=4,(x,-4,4))
# 绘制参数方程
from sympy.plotting import plot_parametric
u= symbols('u')
p1=plot_parametric((cos(u), sin(u)),(u, -5, 5),line_color='black')

from sympy.plotting import plot_parametric
u = symbols('u')
expr1 =(u, cos(2*pi*u)/2 + 1/2)
expr2 =(u, sin(2*pi*u)/2 + 1/2)
p=plot_parametric(expr1, expr2, (u, 0, 1), line_color='blue')
p[1].line_color = 'red'
p.show()

# 绘制三维参数曲线方程
from sympy.plotting import plot3d_parametric_line
u = symbols('u')
plot3d_parametric_line(cos(u), sin(u),u,(u, -10, 10))

# 绘制三维曲面方程
from sympy.plotting import plot3d
x,y= symbols('x y')
plot3d(x**2+y**2,(x,-5,5),(y,-5,5))
'''